package newtec.action;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import newtec.bean.Newtec;
import newtec.factory.NewtecFactory;
import newtec.service.NewtecService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NewtecAction extends ActionSupport implements SessionAware{
	private Newtec Newtec=new Newtec();
	private List<Newtec> Newteclist=new ArrayList<Newtec>();
	private Map session;
	private File image;    //file to upload 
	private String imageFileName; //file name
	private String imageContentType;  //file type
	private InputStream inputStream;
	private String fileName;
	private int pagenow;
	
	public int getPagenow(){
		return pagenow;
	}
    public  void setPagenow(int pagenow){
    	this.pagenow=pagenow;
    }
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	 public File getImage() {
	        return image;
	    }

	    public void setImage(File img) {
	        this.image = img;
	    }

	    public String getImageFileName() {
	        return imageFileName;
	    }

	    public void setImageFileName(String imageFileName) {
	        this.imageFileName = imageFileName;
	    }

	    public String getImageContentType() {
	        return imageContentType;
	    }

	    public void setImageContentType(String imageContentType) {
	        this.imageContentType = imageContentType;
	    }


	public void setNewtec(Newtec Newtecbean){
		this.Newtec=Newtecbean;
	}
	
	public Newtec getNewtec(){
		return Newtec;
	}
	
	public List<Newtec> getNewteclist(){
		return Newteclist;
	}
	
	public void setNewteclist(List<Newtec> Newteclist){
		this.Newteclist=Newteclist;
	}
	
	public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public String modifyNewtec() throws Exception {
		if(Newtec.getTitle().trim().equalsIgnoreCase("")){
			this.addActionError("modifyNewtec error");
			return INPUT;
		}
	   if(Newtec.getContent().trim().equalsIgnoreCase("")){
		   this.addActionError("modifyNewtec error");
		   return INPUT;
	   }

		NewtecService Newtecservice=NewtecFactory.getInstance().creatNewtecService();
        if(!Newtecservice.ModifyNewtec(Newtec)){
        	this.addActionError("modifyNewtec error");
        	return INPUT;
        }
		
		Map session = ActionContext.getContext().getSession();
		session.remove("Newtec");
		return SUCCESS;
	}
	
	public String deleteNewtec() throws Exception {
		NewtecService Newtecservice =NewtecFactory.getInstance().creatNewtecService();
		if (!Newtecservice.DeleteNewtec(Newtec)) {
			this.addActionError("deleteNewtec error");
			return INPUT;
		} else {
			this.addActionError("deleteNewtec error");
			return SUCCESS;
		}
	}
	
	public String addNewtec() throws Exception {	
		if(image!=null){
		Random rnd = new Random();
		int p = rnd.nextInt(20);
		String num = Integer.toString(p);
		String path=ServletActionContext.getServletContext().getRealPath("/");
		path=path.replace("bestcleaner", "images"); 
		imageFileName = num + "." + imageFileName;
		File file = new File(path + "//" + imageFileName);
		imageFileName="/images/"+imageFileName;
		FileUtils.copyFile(image, file);
		}
		else 
			imageFileName="";
		NewtecService Newtecservice=NewtecFactory.getInstance().creatNewtecService();

		if (Newtec.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("addNewtec error");
			return INPUT;
		}
		if (Newtec.getContent().trim().equalsIgnoreCase("")) {
			this.addActionError("addNewtec error");
			return INPUT;
		}
		if(!Newtecservice.AddNewtec(Newtec,imageFileName)){
			this.addActionError("addNewtec error");
			return INPUT;
		}
		this.addActionError("addNewtec error");
		return SUCCESS;
	}

	 public String showallNewtec() throws Exception{
		 NewtecService Newtecservice=NewtecFactory.getInstance().creatNewtecService();
		 Newteclist=Newtecservice.ShowAllNewtec(getPagenow(),6);
		 if(getPagenow()==0){
			 int total=Newtecservice.CountAllNewtec();
			 int page=0;
			 do {
					total = total - 6;
					page++;
				} while (total > 0);
				this.session.put("newtectotalpage", page);
		 }
		 if(Newteclist.size()==0){
			 this.addActionError("showallNewtec error");
			return INPUT;
		 }else{
			 return  SUCCESS; 
		 }
	 }
	 
	 public String showoneNewtec() throws Exception{
			NewtecService NewtecService =NewtecFactory.getInstance().creatNewtecService();
			Newtec= NewtecService.ShowoneNewtec(Newtec);
			if (Newtec == null) {
				this.addActionError("Error!");
				return INPUT;
			} else {
				this.session.put("Newtec", Newtec);
				return SUCCESS;
			}
		}
	 }
